home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dave falkenburg's sprocket / lib / preferences.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  2.8 KB  |  91 lines

  1. /*
  2.     File:        Preferences.cp
  3.  
  4.     Contains:    minimalist preference file routines
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 11/16/94    DRF                Kill a kinda bogus warning by replacing “sizeof(<variable>)”
  21.                                             with “sizeof(<type>)”.
  22.                 9/27/94        DRF                 AppLib.h is now Sprocket.h
  23.                 9/9/94        DRF                Reordered headers and removed redundant #includes.
  24.  
  25.  
  26. */
  27.  
  28. #include "Sprocket.h"
  29.  
  30. #include <Resources.h>
  31. #include <TextUtils.h>
  32. #include <Folders.h>
  33.  
  34. short
  35. OpenPreferencesResFile(void)
  36.     {
  37.     OSErr            err;
  38.     short            prefsVRefNum;
  39.     long            prefsDirID;
  40.     HParamBlockRec    pb;
  41.     short            prefsRefNum;
  42.     Str255            prefsFileName;
  43.     
  44.     err = FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&prefsVRefNum,&prefsDirID);
  45.  
  46.     if (err != noErr)    //    Couldn’t find preferences folder, something is wrong
  47.         return(-1);
  48.  
  49.     GetIndString(prefsFileName,kPreferencesFileStrings,kPreferencesFileName);
  50.  
  51.     //    Try openning the Preferences file
  52.             
  53.     prefsRefNum = HOpenResFile(prefsVRefNum,prefsDirID,prefsFileName,fsRdWrPerm); 
  54.     if (prefsRefNum == -1)
  55.         {
  56.         //    Get the application’s creator
  57.         
  58.         ProcessInfoRec        processInfo;
  59.         ProcessSerialNumber    currentProc = {0,kCurrentProcess};
  60.         
  61.         processInfo.processInfoLength = sizeof(ProcessInfoRec);
  62.         processInfo.processName = nil;
  63.         processInfo.processAppSpec = nil;
  64.         
  65.         (void) GetProcessInformation(¤tProc,&processInfo);
  66.         
  67.         //    Couldn’t open prefs file, try making a new one
  68.         
  69.         HCreateResFile(prefsVRefNum,prefsDirID,prefsFileName);
  70.         pb.fileParam.ioNamePtr = prefsFileName;
  71.         pb.fileParam.ioVRefNum = prefsVRefNum;
  72.         pb.fileParam.ioDirID = prefsDirID;
  73.         pb.fileParam.ioFDirIndex = 0;
  74.         err = PBHGetFInfo(&pb,false);
  75.         
  76.         pb.fileParam.ioNamePtr = prefsFileName;
  77.         pb.fileParam.ioVRefNum = prefsVRefNum;
  78.         pb.fileParam.ioDirID = prefsDirID;
  79.         pb.fileParam.ioFDirIndex = 0;
  80.         pb.fileParam.ioFlFndrInfo.fdType = 'PREF';
  81.         pb.fileParam.ioFlFndrInfo.fdCreator = processInfo.processSignature;
  82.         pb.fileParam.ioFlFndrInfo.fdFlags = 0;
  83.         err = PBHSetFInfo(&pb,false); 
  84.     
  85.         if (ResError() == noErr)    //    Try opening the newly created prefs file if we made it
  86.             prefsRefNum = HOpenResFile(prefsVRefNum,prefsDirID,prefsFileName,fsRdWrPerm);            
  87.         }
  88.         
  89.     return(prefsRefNum);
  90.     }
  91.